package com.zhanghe.study.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * Reduce阶段
 * @author zh
 * @date 2021/3/27 22:35
 */
// 泛型的含义
    //1.map阶段key的类型
    //2.map阶段value的类型
    //3.输出数据key的类型
    //4.输出数据value的类型
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

    IntWritable value = new IntWritable();
    // 用来处理map阶段的结果
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum = 0;
        // 累加求和
        for(IntWritable intWritable : values){
            sum+=intWritable.get();
        }
        value.set(sum);
        context.write(key,value);
    }
}
